package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TGoods;
import com.ruoyi.system.service.ITGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TGoodsCartsMapper;
import com.ruoyi.system.domain.TGoodsCarts;
import com.ruoyi.system.service.ITGoodsCartsService;

/**
 * t_goods_carts 商品购物车主Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-04-09
 */
@Service
public class TGoodsCartsServiceImpl implements ITGoodsCartsService 
{
    @Autowired
    private TGoodsCartsMapper tGoodsCartsMapper;

    @Autowired
    private ITGoodsService itGoodsService;

    /**
     * 查询t_goods_carts 商品购物车主
     * 
     * @param pkId t_goods_carts 商品购物车主主键
     * @return t_goods_carts 商品购物车主
     */
    @Override
    public TGoodsCarts selectTGoodsCartsByPkId(Long pkId)
    {
        return tGoodsCartsMapper.selectTGoodsCartsByPkId(pkId);
    }

    /**
     * 查询t_goods_carts 商品购物车主列表
     * 
     * @param tGoodsCarts t_goods_carts 商品购物车主
     * @return t_goods_carts 商品购物车主
     */
    @Override
    public List<TGoodsCarts> selectTGoodsCartsList(TGoodsCarts tGoodsCarts)
    {
        List<TGoodsCarts> lists= tGoodsCartsMapper.selectTGoodsCartsList(tGoodsCarts);
        for(TGoodsCarts goodsCarts:lists){
            goodsCarts.settGoods(itGoodsService.selectTGoodsByGoodsId(goodsCarts.getGoodsId()));
        }
        return lists;
    }

    /**
     * 新增t_goods_carts 商品购物车主
     * 
     * @param tGoodsCarts t_goods_carts 商品购物车主
     * @return 结果
     */
    @Override
    public int insertTGoodsCarts(TGoodsCarts tGoodsCarts)
    {
        TGoodsCarts query=new TGoodsCarts();
        query.setGoodsId(tGoodsCarts.getGoodsId());
        query.setDeleteFlag("0");
       List<TGoodsCarts> lists= tGoodsCartsMapper.selectTGoodsCartsList(query);
        if(!lists.isEmpty()){
            TGoodsCarts goodsCarts=lists.get(0);
            goodsCarts.setGdsQty(goodsCarts.getGdsQty()+1);
            goodsCarts.setUpdatedTime(DateUtils.getNowDate());
            return tGoodsCartsMapper.updateTGoodsCarts(goodsCarts);
        }else{
            query.setGdsQty(1l);
            query.setCreatedTime(DateUtils.getNowDate());
            return tGoodsCartsMapper.insertTGoodsCarts(query);
        }
    }

    /**
     * 修改t_goods_carts 商品购物车主
     * 
     * @param tGoodsCarts t_goods_carts 商品购物车主
     * @return 结果
     */
    @Override
    public int updateTGoodsCarts(TGoodsCarts tGoodsCarts)
    {
        return tGoodsCartsMapper.updateTGoodsCarts(tGoodsCarts);
    }

    /**
     * 批量删除t_goods_carts 商品购物车主
     * 
     * @param pkIds 需要删除的t_goods_carts 商品购物车主主键
     * @return 结果
     */
    @Override
    public int deleteTGoodsCartsByPkIds(Long[] pkIds)
    {
        return tGoodsCartsMapper.deleteTGoodsCartsByPkIds(pkIds);
    }

    /**
     * 删除t_goods_carts 商品购物车主信息
     * 
     * @param pkId t_goods_carts 商品购物车主主键
     * @return 结果
     */
    @Override
    public int deleteTGoodsCartsByPkId(Long pkId)
    {
        return tGoodsCartsMapper.deleteTGoodsCartsByPkId(pkId);
    }
}
